package com.maistars.spider.infrastructure.adapter;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.logging.Level;

/**
 * @author dhr
 * @date 2020/10/9 1:15 上午
 */
public class HtmlUnitAdapter {

    public static Document monitorChromeToAccessUrl(String url) throws IOException {
        return monitorChromeToAccessUrl(url,5000);
    }

    public static Document monitorChromeToAccessUrl(String url, long javascriptTimeout) throws IOException {
        //构造一个webClient 模拟Chrome 浏览器
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
        //支持JavaScript
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setActiveXNative(false);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.getOptions().setTimeout(20*1000);
        webClient.getCurrentWindow().setInnerHeight(Integer.MAX_VALUE);

        HtmlPage rootPage = webClient.getPage(url);
        //设置一个运行JavaScript的时间
        webClient.waitForBackgroundJavaScript(10*1000);
        String html = rootPage.asXml();
        webClient.close();
        Document document = Jsoup.parse(html);
        return document;
    }
}
